package study.core.rdd.action

import org.apache.spark.rdd.RDD
import org.apache.spark.{SparkConf, SparkContext}

/**
 * 行动算子之countByKey操作
 * 统计每种 key 的个数
 *
 * @author zh
 * @date 2021/5/16 13:32
 */
object TestCountByKey {
  def main(args: Array[String]): Unit = {
    val sparkConf = new SparkConf().setMaster("local[*]").setAppName("rddMap")
    val context = new SparkContext(sparkConf)

    // 准备集合
    val list = List("a","b","c","a","a","c")
    // 创建RDD
    val rdd = context.makeRDD(list, 2)
    println(rdd.dependencies)
    println("-------------")
    val mapRdd = rdd.map((_,1))
    println(mapRdd.dependencies)
    val result = mapRdd.countByKey()


    // Map(b -> 1, a -> 3, c -> 2)
    println(result)

    context.stop()
  }
}
